<?php
/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/11/14
 * Time: 23:00
 */

class Task_aoshu_xcx_model extends MY_Model
{
    function add_task($data)
    {
//        var_dump($data);
//        exit();
        $sid = $_POST['sid'];
        $name = $_POST['name'];
        $class = $_POST['class'];
        $count = $_POST['count'];
        $date = $_POST['date'];
        $sfile = $_POST['file'];

        $sql = /** @lang text */
            "SELECT open_show FROM ws_task_aoshu WHERE sid=$sid AND COUNT=$count AND tenantId=1";

//        $this->db->trans_start();
        $query = $this->db->query($sql);
//        $this->db->trans_complete();
        $error = $this->db->error();
        $row = $query->num_rows();
        if ($row) {//判断数据是否存在
            $cache = $query->result_array()[0]['open_show'];
            if ($cache !== 'null' && $cache !== $sfile) {
                $cache = $cache . $sfile . ';';
                $sql = /** @lang text */
                    "UPDATE ws_task_aoshu SET open_show = '$cache' WHERE sid =$sid AND COUNT =$count";
            } else {
                $sql = /** @lang text */
                    "UPDATE ws_task_aoshu SET open_show = '$sfile' WHERE sid =$sid AND COUNT =$count";
            }
            $query = $this->db->query($sql);
            $error = $this->db->error();
        } elseif ($row < 1) {//数据不存在或图片重复提交
            header($this->config->item('http_status')[500]);
            return false;
        }

        if ($query) {
            return json_encode(array('result' => SUCCESS, 'msg' => $this->config->item('mysql_msg')[$error['code']]));
        }
        return json_encode(array('result' => ERROR, 'msg' => $this->config->item('mysql_msg')[$error['code']], 'code' => $error['code']));
    }
}